package com.guigui.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author ：xin
 * @date ：Created in 2021/7/27 21:55
 * @description：
 */
public class AppointNumberConstraintValidator implements ConstraintValidator<AppointNumberValid, Integer> {
    private int[] vals;
    /**
     * 初始化方法
    * @return: [com.guigui.common.valid.AppointNumberValid]
    * @Author: xin
    * @Date: 2021/7/27 21:56
    */
    @Override
    public void initialize(AppointNumberValid constraintAnnotation) {
        //注解中的vals传值到这里
        vals = constraintAnnotation.vals();
    }
    /**
     *判断是否校验成功
     * @param value 需要校验的值
     * @param context 校验上下文环境信息
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        Set<Integer> val = new HashSet<>();
        if (vals.length > 0){
            for (int v : vals) {
                val.add(v);
            }
        }
        return val.contains(value);
    }
}
